﻿using AppForm_Managers.Object_Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppForm_Managers.Views
{
    public partial class Location_Manager : Form
    {
        public Location_Manager()
        {
            InitializeComponent();
        }

        private void RefeshButton_Click(object sender, EventArgs e)
        {
            GridProvinces_Load(sender, e);
            GridDistricts.DataSource = null;
            GridCommunes.DataSource = null;
            GridOgrans.DataSource = null;
        }

        private void GridProvinces_Load(object sender, EventArgs e)
        {
            ManagerEmployeesEntities db = new ManagerEmployeesEntities();
            try
            {
                GridProvinces.DataSource = db.Provinces.ToList();
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void ViewProvinces_Click(object sender, EventArgs e)
        {
            GridDistricts.DataSource = null;
            GridCommunes.DataSource = null;
            GridOgrans.DataSource = null;
            ManagerEmployeesEntities db = new ManagerEmployeesEntities();
            try
            {
                int provId = 0;
                List<int> listSelected = ViewProvinces.GetSelectedRows().ToList<int>();
                int index = -1;
                foreach (var item in listSelected)
                {
                    if (item > -1)
                    {
                        index = item;
                        break;
                    }
                }
                if (index > -1)
                {
                    provId = Int32.Parse(ViewProvinces.GetRowCellValue(index, "ProvID").ToString());
                    Province prv = db.Provinces.Single(p => p.ProvID == provId);
                    GridDistricts.DataSource = prv.Districts.ToList();
                    GridCommunes.DataSource = null;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }

        private void ViewDistricts_Click(object sender, EventArgs e)
        {
            GridCommunes.DataSource = null;
            GridOgrans.DataSource = null;
            ManagerEmployeesEntities db = new ManagerEmployeesEntities();
            try
            {
                int distId = 0;
                List<int> listSelected = ViewDistricts.GetSelectedRows().ToList<int>();
                int index = -1;
                foreach (var item in listSelected)
                {
                    if (item > -1)
                    {
                        index = item;
                        break;
                    }
                }
                if (index > -1)
                {
                    distId = Int32.Parse(ViewDistricts.GetRowCellValue(index, "DistID").ToString());
                    District prv = db.Districts.Single(d => d.DistID == distId);
                    GridCommunes.DataSource = prv.Communes.ToList();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }

        private void ViewCommunes_Click(object sender, EventArgs e)
        {
            GridOgrans.DataSource = null;
            ManagerEmployeesEntities db = new ManagerEmployeesEntities();
            try
            {
                List<int> listSelected = ViewCommunes.GetSelectedRows().ToList<int>();
                int index = -1;
                foreach (var item in listSelected)
                {
                    if (item > -1)
                    {
                        index = item;
                        break;
                    }
                }
                if (index > -1)
                {
                    int comID = Int32.Parse(ViewCommunes.GetRowCellValue(index, "ComID").ToString());
                    Commune commune = db.Communes.SingleOrDefault(p => p.ComID == comID);
                    GridOgrans.DataSource = commune.Organizations.ToList();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
    }
}